package com.example.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.ArrayList;
import java.util.List;

public class SingelShowFragment extends Fragment {

    private XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    private XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    private LinearLayout lineshow;

    private List<XYSeries> line = new ArrayList<>();

    //数据集
    ArrayList<Float> line0 = new ArrayList<>();
    ArrayList<Float> line1 = new ArrayList<>();
    ArrayList<Float> line2 = new ArrayList<>();
    ArrayList<Float> line3 = new ArrayList<>();
    ArrayList<Float> line4 = new ArrayList<>();
    ArrayList<Float> line5 = new ArrayList<>();
    ArrayList<Float> line6 = new ArrayList<>();
    ArrayList<Float> line7 = new ArrayList<>();
    ArrayList<Float> line8 = new ArrayList<>();
    ArrayList<Float> line9 = new ArrayList<>();
    ArrayList<Float> line10 = new ArrayList<>();
    ArrayList<Float> line11 = new ArrayList<>();
    ArrayList<Float> line12 = new ArrayList<>();
    ArrayList<Float> line13 = new ArrayList<>();
    ArrayList<Float> line14 = new ArrayList<>();
    ArrayList<Float> line15 = new ArrayList<>();


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        /**
         * 这里主要初始化一些在Fragment需使用和持久化的必要组件
         */
        super.onCreate(savedInstanceState);
        //初始化曲线样式
        initline();
        //初始化曲线数据
        initlineData();

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        /**
         * 使用inflate()方法加载一个自定义的layout布局，该布局是Fragment的跟布局view
         * inflate()方法的3个参数
         * 1.想要加载的layout Resource Id
         * 2.加载layoutde父ViewGroup ，目的是将layout挂靠到container上
         * 3.布尔值，指示是否将layout附着到VieGroup上，这里一般指定false
         * 因为layout已经附着到container上，若为true，系统将为layout新建一个ViewGroupzuop作为对象，多余
         */
        View inView = inflater.inflate(R.layout.activity_singelshow_fragment, container, false);
        /**
         * 这里主要是初始化layout中的控件对象
         */
        return inView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lineshow = (LinearLayout) getActivity().findViewById(R.id.line_main);
        lineshow.addView(ChartFactory.getLineChartView(getContext(), dataset, renderer));
}

    @Override
    public void onPause() {
        super.onPause();
        /**
         * 当用户离开此Fragment界面时，系统调用此方法，这里主要保存一些持久化的对象状态
         */
    }

    //设置曲线样式
    private void initline() {

        float width = 1f;

        XYSeriesRenderer xySeriesRenderer0 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer1 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer2 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer3 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer4 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer5 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer6 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer7 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer8 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer9 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer10 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer11 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer12 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer13 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer14 = new XYSeriesRenderer();
        XYSeriesRenderer xySeriesRenderer15 = new XYSeriesRenderer();

        xySeriesRenderer0.setColor(R.color.line0);
        xySeriesRenderer0.setLineWidth(width);
        xySeriesRenderer0.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer0);

        xySeriesRenderer1.setColor(R.color.line1);
        xySeriesRenderer1.setLineWidth(width);
        xySeriesRenderer1.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer1);

        xySeriesRenderer2.setColor(R.color.line2);
        xySeriesRenderer2.setLineWidth(width);
        xySeriesRenderer2.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer2);

        xySeriesRenderer3.setColor(R.color.line3);
        xySeriesRenderer3.setLineWidth(width);
        xySeriesRenderer3.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer3);

        xySeriesRenderer4.setColor(R.color.line4);
        xySeriesRenderer4.setLineWidth(width);
        xySeriesRenderer4.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer4);

        xySeriesRenderer5.setColor(R.color.line5);
        xySeriesRenderer5.setLineWidth(width);
        xySeriesRenderer5.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer5);

        xySeriesRenderer6.setColor(R.color.line6);
        xySeriesRenderer6.setLineWidth(width);
        xySeriesRenderer6.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer6);

        xySeriesRenderer7.setColor(R.color.line7);
        xySeriesRenderer7.setLineWidth(width);
        xySeriesRenderer7.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer7);

        xySeriesRenderer8.setColor(R.color.line8);
        xySeriesRenderer8.setLineWidth(width);
        xySeriesRenderer8.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer8);

        xySeriesRenderer9.setColor(R.color.line9);
        xySeriesRenderer9.setLineWidth(width);
        xySeriesRenderer9.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer9);

        xySeriesRenderer10.setColor(R.color.line10);
        xySeriesRenderer10.setLineWidth(width);
        xySeriesRenderer10.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer10);

        xySeriesRenderer11.setColor(R.color.line11);
        xySeriesRenderer11.setLineWidth(width);
        xySeriesRenderer11.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer11);

        xySeriesRenderer12.setColor(R.color.line12);
        xySeriesRenderer12.setLineWidth(width);
        xySeriesRenderer12.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer12);

        xySeriesRenderer13.setColor(R.color.line13);
        xySeriesRenderer13.setLineWidth(width);
        xySeriesRenderer13.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer13);

        xySeriesRenderer14.setColor(R.color.line14);
        xySeriesRenderer14.setLineWidth(width);
        xySeriesRenderer14.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer14);

        xySeriesRenderer15.setColor(R.color.line15);
        xySeriesRenderer15.setLineWidth(width);
        xySeriesRenderer15.setPointStyle(PointStyle.POINT);
        renderer.addSeriesRenderer(xySeriesRenderer15);

        //设置图样式
        renderer.setZoomEnabled(false);                 //不可缩放
        renderer.setPanEnabled(true, false);    //x轴可拉伸，y轴补课拉伸
        renderer.setShowLegend(false);          //不显示图例
        renderer.setMargins(new int[]{20, 40, 20, 10});//上、左、下、右   设置入外边距
        renderer.setShowGrid(true);         //显示网格
        renderer.setGridColor(Color.WHITE);     //网格颜色为白色
        renderer.setApplyBackgroundColor(true);     //允许设置背景
        renderer.setBackgroundColor(Color.BLACK);   //背景为黑色
        renderer.setXTitle("Time/h");               //设置x轴标题
        renderer.setAxisTitleTextSize(20);          //标题字体大小
        renderer.setXLabels(15);
        renderer.setXAxisMin(0.0);
        renderer.setYTitle("Ln/(光度值)");
        renderer.setYLabelsPadding(10);
        renderer.setYLabels(5);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(100);
    }

    public void initlineData() {
        dataset.clear();
        //定义曲线
        XYSeries series0 = new XYSeries(" ");
        XYSeries series1 = new XYSeries(" ");
        XYSeries series2 = new XYSeries(" ");
        XYSeries series3 = new XYSeries(" ");
        XYSeries series4 = new XYSeries(" ");
        XYSeries series5 = new XYSeries(" ");
        XYSeries series6 = new XYSeries(" ");
        XYSeries series7 = new XYSeries(" ");
        XYSeries series8 = new XYSeries(" ");
        XYSeries series9 = new XYSeries(" ");
        XYSeries series10 = new XYSeries(" ");
        XYSeries series11 = new XYSeries(" ");
        XYSeries series12 = new XYSeries(" ");
        XYSeries series13 = new XYSeries(" ");
        XYSeries series14 = new XYSeries(" ");
        XYSeries series15 = new XYSeries(" ");

        line.add(series0);
        line.add(series1);
        line.add(series2);
        line.add(series3);
        line.add(series4);
        line.add(series5);
        line.add(series6);
        line.add(series7);
        line.add(series8);
        line.add(series9);
        line.add(series10);
        line.add(series11);
        line.add(series12);
        line.add(series13);
        line.add(series14);
        line.add(series15);

        for (int i = 0; i < 16; i++) {
            addlinedata(line.get(i), i);
        }

        dataset.addAllSeries(line);
    }

    //把数组的数加入到每条曲线
    public void addlinedata(XYSeries xySeries, int index) {
        float time = 5;         //事件间隔
        switch (index) {
            case 0:
                for (int i = 0; i < line0.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 1:
                for (int i = 0; i < line1.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 2:
                for (int i = 0; i < line2.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 3:
                for (int i = 0; i < line3.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 4:
                for (int i = 0; i < line4.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 5:
                for (int i = 0; i < line5.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 6:
                for (int i = 0; i < line6.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 7:
                for (int i = 0; i < line7.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 8:
                for (int i = 0; i < line8.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 9:
                for (int i = 0; i < line9.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 10:
                for (int i = 0; i < line10.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 11:
                for (int i = 0; i < line11.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 12:
                for (int i = 0; i < line12.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 13:
                for (int i = 0; i < line13.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 14:
                for (int i = 0; i < line14.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
            case 15:
                for (int i = 0; i < line15.size(); i++) {
                    xySeries.add(i * time, line0.get(i));
                }
                break;
        }
    }
}
